﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DFControlLibrary
{
    class BorderDrawer
    {
        const int FrameColorR = 255;
        const int FrameColorG = 255;
        const int FrameColorB = 255;
        
        // 文字のフォント
        private static Font _sFont = new Font("Arial", 16);
        // 文字のブラシ
        private static SolidBrush _sFontBrush = new SolidBrush(Color.Black);
        // 目標笑顔度以上場合文字のブラシ
        private static SolidBrush _sHighFontBrush = new SolidBrush(Color.Red);
        // 枠の背景色
        private static　SolidBrush _sFrameBrush = new SolidBrush(Color.FromArgb(200, FrameColorR, FrameColorG, FrameColorB));	//枠の色？
        //枠の縁取り
        private static　Pen _sFramePen = new Pen(Color.FromArgb(200, FrameColorR, FrameColorG, FrameColorB));
        
        public BorderDrawer()
        {
        }

        ~BorderDrawer()
        {
        }

        /// <summary>
        /// 笑顔度と顔枠を描画する
        /// </summary>
        /// <param name="bitmap">元画像</param>
        /// <returns></returns>
        static public bool DrawScoreAndBorder(Image bitmap, FaceInfoList lstFace)
        {
            if ((bitmap == null) || (lstFace == null) || (lstFace.Count == 0))
                return false;

            FaceInfo info = lstFace[0];
            Graphics offScreen = Graphics.FromImage(bitmap);	//描画先
            offScreen.SmoothingMode = SmoothingMode.HighQuality;
            
            Point		cpNW = new Point();
            Point		cpNE = new Point();
            Point		cpSE = new Point();
            Point		cpSW = new Point();

            const int iframewidth = 5;	// フレーム枠の幅            
            int idiameter = info.Point.Width / 2 > 80 ? 80 : info.Point.Width / 2;	// 円角のサイズ
            const int ititlewidth = 30;	// タイトルバーの高さ
            int		l=0;	// 笑顔度


            cpNW.X = info.Point.Left;
            cpNE.X = info.Point.Right;
            cpSE.X = info.Point.Right;
            cpSW.X = info.Point.Left;
            cpNW.Y = info.Point.Top;
            cpNE.Y = info.Point.Top;
            cpSE.Y = info.Point.Bottom;
            cpSW.Y = info.Point.Bottom;
            l = info.SmileValue;
            
            // 外枠
            GraphicsPath gpFrame = new GraphicsPath();
            // 内枠
            GraphicsPath gpWindow = new GraphicsPath();

	        // Define the Frame.
	        gpFrame.StartFigure();
	        gpFrame.AddArc( cpNW.X-iframewidth, cpNW.Y-iframewidth-ititlewidth
		        , idiameter+iframewidth*2, idiameter+iframewidth*2
		        , 180.0f, 90.0f);
	        gpFrame.AddArc( cpNE.X-idiameter-iframewidth, cpNE.Y-iframewidth-ititlewidth
		        , idiameter+iframewidth*2, idiameter+iframewidth*2
		        , 270.0f, 90.0f);
	        gpFrame.AddArc( cpSE.X-idiameter-iframewidth, cpSE.Y-idiameter-iframewidth
		        , idiameter+iframewidth*2, idiameter+iframewidth*2
		        , 0.0f, 90.0f);
	        gpFrame.AddArc( cpSW.X-iframewidth, cpSW.Y-idiameter-iframewidth
		        , idiameter+iframewidth*2, idiameter+iframewidth*2
		        , 90.0f, 90.0f);
	        gpFrame.CloseFigure();

	        // Define the Window.
	        gpWindow.StartFigure();
	        gpWindow.AddArc(cpNW.X, cpNW.Y,idiameter,idiameter,180.0f,90.0f);
	        gpWindow.AddArc(cpNE.X-idiameter, cpNE.Y,idiameter, idiameter,270.0f, 90.0f);
	        gpWindow.AddArc(cpSE.X-idiameter,cpSE.Y-idiameter,idiameter, idiameter,0.0f, 90.0f);
	        gpWindow.AddArc(cpSW.X,cpSW.Y-idiameter,idiameter,idiameter,90.0f, 90.0f);
	        gpWindow.CloseFigure();

	        // Define the Region.
	        Region rgFrame = new Region(gpFrame);

	        // Add region.
	        rgFrame.Exclude(gpWindow);

            offScreen.FillRegion(_sFrameBrush, rgFrame);
	        offScreen.DrawPath(_sFramePen, gpFrame);
            offScreen.DrawPath(_sFramePen, gpWindow);

            gpFrame.Dispose();
            gpWindow.Dispose();
            rgFrame.Dispose();

            string wcTitle = string.Format("SMILE:{0,3}", l);
	        
            // 笑顔度描画
	        RectangleF rcLFont = new RectangleF(cpNW.X, cpNW.Y-ititlewidth, cpNE.X - cpNW.X, ititlewidth );
	        StringFormat sfFont = new StringFormat();
            sfFont.Alignment = StringAlignment.Center;
            if (l > Constants.TARGET_SMILE_VALUE)
                offScreen.DrawString(wcTitle.ToString(), _sFont, _sHighFontBrush, rcLFont, sfFont);
            else
                offScreen.DrawString(wcTitle.ToString(), _sFont, _sFontBrush, rcLFont, sfFont);

	        LinearGradientBrush lgLBrush = new LinearGradientBrush(new PointF(cpNW.X+idiameter/2, cpNW.Y), 
                new PointF(cpNE.X-idiameter/2, cpNE.Y),
                Color.FromArgb(125,0,255,100),
                Color.FromArgb(255,0,0,255));

	        GraphicsPath gLPath = new GraphicsPath();
	        PointF[] Lpoints = new PointF[]{
		        new PointF(cpNW.X+idiameter/2, cpNW.Y)
		        , new PointF(cpNW.X + idiameter/2+ (cpNE.X - cpNW.X - idiameter) * l/100, cpNE.Y)
		        , new PointF(cpNW.X + idiameter/2 + (cpNE.X - cpNW.X - idiameter)* l /100, cpNE.Y-((ititlewidth/3)*l/100))
	        };
	        gLPath.AddPolygon(Lpoints);
	        offScreen.FillPath(lgLBrush, gLPath);

            lgLBrush.Dispose();
            gLPath.Dispose();
            sfFont.Dispose();
            offScreen.Dispose(); 
            return true;
        }
    }
}
